/*
 *所有关于tb_project_participationer类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_participationerService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Entity.DomainModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Collections.Generic;
using Mapster;
using System;
using MYData.Entity.DTO.ApiEntity.Input;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.Linq;
using NuGet.Packaging;
using MYData.Core.EFDbContext;

namespace MYData.System.Services
{
    public partial class tb_project_participationerService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_participationerRepository _repository;//访问数据库
        private readonly Itb_project_projectpersonnelRepository _projectpersonnel;//访问参与人员数据库
        private readonly VOLContext _db;

        [ActivatorUtilitiesConstructor]
        public tb_project_participationerService(
            Itb_project_participationerRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            Itb_project_projectpersonnelRepository projectpersonnel,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _projectpersonnel = projectpersonnel;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        /// <summary>
        /// 项目参与人信息表多添加与项目立项基本信息与参与人员关系表添加
        /// </summary>
        /// <param name="list"></param>
        public void AddList(IEnumerable<tb_project_participationer_Dto> list,string projectId)
        {
            tb_project_projectpersonnelDto RelationDto = new tb_project_projectpersonnelDto();
            var model = list.Adapt<IEnumerable<tb_project_participationer>>();
            //var keys = "";
            foreach (var item in model)
            {
                //var key= new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                var key = Guid.NewGuid().ToString();
                item.participantsId = key;
                item.projectId = projectId;
                _repository.Add(item, false);
                RelationDto.projectId = projectId;
                RelationDto.personnelId = key;
                var RelationModel = RelationDto.Adapt<tb_project_projectpersonnel>();
                var key1 = Guid.NewGuid().ToString();
                RelationModel.id = key1;
                _projectpersonnel.Add(RelationModel, false);
            }
        }
        /// <summary>
        /// 项目参与人信息表多修改与项目立项基本信息与参与人员关系表修改
        /// </summary>
        /// <param name="list"></param>
        public void UptList(IEnumerable<tb_project_participationerUpdate_Dto> Uptlist, string projectId)
        {
            //参与人员关系表Dto
            tb_project_projectpersonnelDto RelationDto = new tb_project_projectpersonnelDto();
            //项目参与人信息表DTO转换
            var model = Uptlist.Adapt<IEnumerable<tb_project_participationer>>();
            //查询与这个项目ID有关的所有关系表数据
            var RelationList = _db.Set<tb_project_projectpersonnel>().Where(x => x.projectId == projectId).ToList();
            foreach (var item in RelationList)
            {
                //根据主键查询每一条关系表数据
                var DelList = _db.Set<tb_project_projectpersonnel>().Where(x => x.id == item.id).FirstOrDefault();
                //单个删除没有用到事务所以写True(清空关系表数据)
                _projectpersonnel.Delete(DelList,false);
            }
            var DelList1 = _db.Set<tb_project_participationer>().Where(x => x.projectId == projectId).ToList();
            //单个删除(清空项目参与人信息表数据)
            foreach (var item1 in DelList1)
            {
                _repository.Delete(item1, false);
            }
            //重新添加两表
            foreach (var item in model)
            {
                //根据主键查询每一条项目参与人信息表数据
                
                
                //重新添加项目参与人信息表数据
                var key = Guid.NewGuid().ToString();
                item.participantsId = key;
                _repository.Add(item, false);
                //提前添加外键防止被覆盖
                RelationDto.projectId = projectId;
                RelationDto.personnelId = key;
                //关系表添加DTO转换
                var RelationModel = RelationDto.Adapt<tb_project_projectpersonnel>();
                //重新生成主键ID再赋值防止覆盖
                var key1 = Guid.NewGuid().ToString();
                RelationModel.id = key1;
                _projectpersonnel.Add(RelationModel, false);
            }


        }



    }
}
